//
//  TestViewController.m
//  SEGStandard
//
//  Created by 山刘 on 2019/4/12.
//  Copyright © 2019年 山刘. All rights reserved.
//

#import "TestViewController.h"

@interface TestViewControllerDataSource : NSObject
//存放外部数据源
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *action;

-(instancetype)initName:(NSString*)name action:(NSString*)action;
@end

@implementation TestViewControllerDataSource
-(instancetype)initName:(NSString*)name action:(NSString*)action {
    if (self == [super init]) {
        _name = name;
        _action = action;
    }
    return self;
}
@end

@interface TestViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
// 数据源
@property (nonatomic, strong)NSArray<TestViewControllerDataSource*> *dataSource;
@end

@implementation TestViewController{
    NSString *defaultTableViewCellIdentifier;
}

#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initViews];
    
    [[SEGLoginV2Manager sharedManager] requestLoginWithPassword:@"222222" tel:@"18816820401" completed:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
    
        if (isSuccessed) {
            [[SEGProfileManager sharedManager] requestProfilecompleted:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                if (!isSuccessed) {
                    [SEGAlertView showTmepMessageWithMessage:errorMessage];
                }else{
                    
                    [SEGHTTPBusinessManager seg_serRequestHeaderDictionary:@{
                                                                             @"systemVersionCode":@"11.4.1",
                                                                             @"phoneModel":@"iPhone6",
                                                                             @"version":@"1.7.0",
                                                                             @"SYSCODE":@"10",
                                                                             @"communityId":@"67",
                                                                             @"source":@"2",
                                                                             @"platform":@"SEGI",
                                                                             @"deviceId":@"FCACB98B-53CA-4314-AB10-56B6E7D2AA1B",
                                                                             @"versionCode":@"41"
                                                                             }];
                    
                    self.dataSource = @[
                                        
                                        [[TestViewControllerDataSource alloc]initName:@"启动广告" action:@"1"],
                                        [[TestViewControllerDataSource alloc]initName:@"我的收藏" action:@"2"],
                                        [[TestViewControllerDataSource alloc]initName:@"邀请邻居" action:@"3"],
                                        
                                        [[TestViewControllerDataSource alloc]initName:@"跳转到登录界面" action:@"4-1"],
                                        [[TestViewControllerDataSource alloc]initName:@"选择小区" action:@"4-2"],
                                        [[TestViewControllerDataSource alloc]initName:@"切换小区" action:@"4-3"],
                                        [[TestViewControllerDataSource alloc]initName:@"邀请邻居" action:@"4-4"],
                                        
                                        [[TestViewControllerDataSource alloc]initName:@"消息" action:@"5-1"],
                                        
                                        [[TestViewControllerDataSource alloc]initName:@"活动红包1" action:@"6-1"],
                                        [[TestViewControllerDataSource alloc]initName:@"活动红包2" action:@"6-2"],
                                        
                                        [[TestViewControllerDataSource alloc]initName:@"版本升级" action:@"7-1"],
                                        
                                        [[TestViewControllerDataSource alloc]initName:@"完善信息" action:@"8-1"],
                                        [[TestViewControllerDataSource alloc]initName:@"帮助中心，等级说明" action:@"8-2"],
                                        [[TestViewControllerDataSource alloc]initName:@"配送地址" action:@"8-3"],
                                        [[TestViewControllerDataSource alloc]initName:@"完善信息" action:@"8-4"],
                                        
                                        [[TestViewControllerDataSource alloc]initName:@"设置" action:@"9-1"],
                                        
                                        [[TestViewControllerDataSource alloc]initName:@"消息列表" action:@"10-1"]
                                        ];
                    
                    [self.tableView reloadData];
                    [SEGAlertView showTmepMessageWithMessage:@"good"];
                }
            }];
        }else{
            [SEGAlertView showTmepMessageWithMessage:errorMessage];
        }
    }];
    
    
    
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
     [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
     [super viewDidDisappear:animated];
}

#pragma mark - private methods
// 初始化视图
-(void)initViews{
    defaultTableViewCellIdentifier = @"DefaultTableViewCellIdentifier";
    [self.view addSubview:self.tableView];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
     return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:defaultTableViewCellIdentifier];
    if (!cell) cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:defaultTableViewCellIdentifier];
    if (self.dataSource.count > indexPath.section) {
        cell.textLabel.text = self.dataSource[indexPath.row].name;
    }
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44.0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UIViewController *vc;
    
    if (self.dataSource.count > indexPath.section) {
        if ([self.dataSource[indexPath.row].action isEqualToString:@"1"]) {
            NSDictionary *params = @{@"position":@(SEGAdvlistType_START),@"vc":self};
            [[SEGUhomeMediator sharedInstance]seg_showLaunchAndPopWindowAdvertisement:params];
        }
        
        if ([self.dataSource[indexPath.row].action isEqualToString:@"2"]) {
            vc = [[SEGUhomeMediator sharedInstance]seg_initMyCollectionViewController:nil];
        }
        
        if ([self.dataSource[indexPath.row].action isEqualToString:@"3"]) {
            vc = [[SEGUhomeMediator sharedInstance]seg_initInviteViewController:nil];
        }
        
        if ([self.dataSource[indexPath.row].action isEqualToString:@"4-1"]) {
            UIViewController *VC = [[SEGUhomeMediator sharedInstance]seg_initLoginViewController:nil];
            [UIApplication sharedApplication].keyWindow.rootViewController = [[UINavigationController alloc] initWithRootViewController:VC];
        }
        
        if ([self.dataSource[indexPath.row].action isEqualToString:@"4-2"]) {
            vc = [[SEGUhomeMediator sharedInstance] seg_initSelectCommunityViewController:nil];
        }
        
        if ([self.dataSource[indexPath.row].action isEqualToString:@"4-3"]) {
            vc = [[SEGUhomeMediator sharedInstance] seg_initChangeCommunityViewController:nil];
        }
        
        
        if ([self.dataSource[indexPath.row].action isEqualToString:@"5-1"]) {
            
            
            
            NSDictionary *tmp = @{
                                  @"ommunityId"           : @"67",
                                  @"communityName"        : @"",
                                  @"createdDate"          : @"0",
                                  @"serviceNbr"           : @"fixedGroup_67_67",
                                  @"serviceNumberDesc"    : @"",
                                  @"serviceNumberId"      : @"67",
                                  @"serviceNumberLogo"    : @"",
                                  @"serviceNumberName"    : @"海文花园服务中心",
                                  @"status" : @"1"
                                  };
            vc = [[SEGUhomeMediator sharedInstance]seg_initServiceMessageViewController:@{@"serviceBaseInfoModel":tmp}];
        }
        
        if ([self.dataSource[indexPath.row].action isEqualToString:@"6-1"]) {
            
            [[SEGAwardManager sharedManager] requestCheckActivityAfterOpenDoor:^(BOOL isSucceed, NSString *errorMessage, SEGAwardForwardType frowardType, NSDictionary *dicData) {
                if (isSucceed) {
                    [[SEGUhomeMediator sharedInstance] seg_dealWithCheckActivityResultDict:@{
                                                                                             @"dicData":dicData,
                                                                                             @"frowardType":@(SEGAwardForwardType_AD),
                                                                                             @"nav": self.navigationController,
                                                                                             @"fromType":@(SEGRedWalletFromType_OpenDoor),
                                                                                             @"vc": self
                                                                                             }];
                }
            }];
        }
        
        if ([self.dataSource[indexPath.row].action isEqualToString:@"6-2"]) {
            [[SEGUhomeMediator sharedInstance] seg_showRedWalletViewWithType:@{
                                                                               @"nav": self.navigationController,
                                                                               @"fromType":@(SEGRedWalletFromType_OpenDoor)
                                                                               }];
        }
        
        if ([self.dataSource[indexPath.row].action isEqualToString:@"7-1"]) {
            vc = [[SEGUhomeMediator sharedInstance] seg_initNewVersionViewController:nil];
        }
        
        if ([self.dataSource[indexPath.row].action isEqualToString:@"8-1"]) {
            NSDictionary *var = @{@"requestUserId":kAccountUserID};
            vc = [[SEGUhomeMediator sharedInstance]seg_initSEGPersonalHomePageNewViewController:var];
        }
        if ([self.dataSource[indexPath.row].action isEqualToString:@"8-2"]) {
            NSDictionary *params = @{@"linkURL":kIntegarlHelpCenter};
            vc = [[SEGUhomeMediator sharedInstance] seg_initSEGMyLevelViewController:params];
            vc.title = @"帮助中心";
        }
        
        if ([self.dataSource[indexPath.row].action isEqualToString:@"8-3"]) {
            vc = [[SEGUhomeMediator sharedInstance]seg_initSEGCompleteGenderInfoViewController:nil];
        }
        if ([self.dataSource[indexPath.row].action isEqualToString:@"8-4"]) {
            NSDictionary *params = @{@"showType":@(AdressListViewControllerType_Order),@"delegate":self,@"addressSelected":[SEGProfileManager sharedManager].profile.addressId ,@"shouldShowConfirmView":@"",@"winnerId":@""};
            vc = [[SEGUhomeMediator sharedInstance]seg_initSEGAdressListViewController:params];
        }
        if ([self.dataSource[indexPath.row].action isEqualToString:@"9-1"]) {
            vc = [[SEGUhomeMediator sharedInstance]seg_initSettingViewController:nil];
        }
        if ([self.dataSource[indexPath.row].action isEqualToString:@"10-1"]) {
            NSDictionary *params = @{@"menuSid":@"206",
                                     @"action":@"TAB_MESSAGE",
                                     @"settingsId":@"10021"};
            vc = [[SEGUhomeMediator sharedInstance] seg_initMessageRootViewController:params];
        }
        
        
    }
    vc.hidesBottomBarWhenPushed = YES;
    if (vc)[self.navigationController pushViewController:vc animated:YES];
}

-(UIViewController *)currentVC {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    if (window.windowLevel != UIWindowLevelNormal) {
        for (UIWindow *tmp in [UIApplication sharedApplication].windows) {
            if (tmp.windowLevel == UIWindowLevelNormal) {
                window = tmp;
                break;
            }
        }
    }
    UIViewController *result = window.rootViewController;
    if (result.presentedViewController) {
        result = result.presentedViewController;
    }
    if ([result isKindOfClass:[UINavigationController class]]) {
        result = [(UINavigationController*)result visibleViewController];
    }
    if ([result isKindOfClass:[UITabBarController class]]) {
        result = [(UITabBarController*)result selectedViewController];
    }
    return result;
}

#pragma mark - Getter
- (UITableView*)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:defaultTableViewCellIdentifier];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        //_tableView.backgroundColor = kColor_bg;
        //_tableView.separatorColor = kColor_L2;
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        if(@available(iOS 11.0, *)){
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _tableView.estimatedSectionHeaderHeight  = _tableView.estimatedSectionHeaderHeight > 0 ? _tableView.estimatedSectionHeaderHeight : 0;
            _tableView.estimatedSectionFooterHeight  = _tableView.estimatedSectionFooterHeight > 0 ? _tableView.estimatedSectionFooterHeight : 0;
        }
    }
    return _tableView;
}

-(NSArray<TestViewControllerDataSource *> *)dataSource{
    if (!_dataSource) {
        _dataSource = [[NSArray alloc] init];
    }
    return _dataSource;
}
#pragma mark - Setter

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
